﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace SRAppClient
{
    public partial class ErrorForm : Form
    {
        private Exception m_Exception;

        public ErrorForm(Exception e)
        {
            m_Exception = e;
            this.TopMost = true;
            InitializeComponent();
        }
        private string m_ErrorMsg;

        public string ErrorMsg
        {
            get {
                if (string.IsNullOrEmpty(m_ErrorMsg))
                    m_ErrorMsg = FormatError(m_Exception);
                return m_ErrorMsg; 
            }
            
        }
        private void ckLokeErrorInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (ckLokeErrorInfo.Checked)
            {
                this.Height = 422;
            }
            else
            {
                this.Height = 244;
            }
        }

        private string FormatError(Exception e)
        {
            string errstr = string.Empty;
            if (e is FileNotFoundException)
            {
                errstr = "程序的依赖项没有找到，请检查程序是否完整";
            }
            else if (e is NullReferenceException || e is ArgumentOutOfRangeException || e is InvalidCastException)
            {
                errstr = "程序内部出现错误";
            }
            else if (e is FileLoadException)
            {
                errstr = "程序版本不正确，请重新下载或者安装程序";
            }
            else
            {
                errstr = e.Message;
            }
            return errstr;
        }

        public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            UnhandledException(e.ExceptionObject as Exception);
        }
        public static void OnThreadExceptionEventHandler(object sender, ThreadExceptionEventArgs e)
        {
            ThreadExceptionEventHandler(e.Exception);
        }
        public static void UnhandledException(Exception e)
        {
            if (e == null) return;
            try
            {
                ErrorForm erForm = new ErrorForm(e);
                erForm.ShowDialog();
            }
            catch
            {

            }
        }
        public static void ThreadExceptionEventHandler(Exception e)
        {
            if (e == null) return;
            try
            {
                ErrorForm erForm = new ErrorForm(e);
                erForm.ShowDialog();
            }
            catch 
            {

                
            }
        }

        private void ErrorForm_Load(object sender, EventArgs e)
        {
            string lp = Environment.NewLine;
            lblErrorMsg.Text = ErrorMsg;
            if (m_Exception.InnerException != null)
            {
                tbErrorMsg.Text = "异常描述：" + m_Exception.InnerException.Message + lp;
            }
            else
                tbErrorMsg.Text = "异常描述：" + m_Exception.Message + lp;
            tbErrorMsg.Text += "异常类型："+m_Exception.GetType().FullName + lp;
            tbErrorMsg.Text += "发生源：" + m_Exception.Source + lp;
            tbErrorMsg.Text += "目标位置：" + m_Exception.TargetSite.Name+lp;
            tbErrorMsg.Text += "堆栈跟踪" + m_Exception.StackTrace + lp;

        }

        private void btCancelErrorMsg_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Ignore;
            this.Close();
        }
    }
}
